package com.dianrui.fmi.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dianrui.fmi.R;
import com.dianrui.fmi.bean.Search;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SearchAdapter extends BaseAdapter {

    private List<Search> list;
    private Context context;
    private SearchClickListener searchClickListener;

    public SearchAdapter(Context context, List<Search> list) {
        this.list = list;
        this.context = context;
    }

    public interface SearchClickListener {
        void onRegularClick(long tick, String name, String symbolsId, String type, String symbol, String fullsymbol, String price, String high, String low, String open, String zf, String yc, String buy1, String buy2, String position, String servicecharge, String zd);

    }

    public void setOnButtonClickListener(SearchClickListener searchClickListener) {
        this.searchClickListener = searchClickListener;
    }

    public void refresh(List<Search> list) {
        this.list = list;
        this.notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        if (list == null) {
            return 0;
        }
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView != null) {
            holder = (ViewHolder) convertView.getTag();
        } else {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_search, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        if (null != list && list.size() > 0) {
            final Search search = list.get(position);
            holder.name.setText(search.full_symbol);
            holder.fullsymbol.setText(search.symbol);
            holder.price.setText(search.price);
            holder.zf.setText(search.zf + "%");
            if (search.zf.contains("-")) {
                holder.zf.setBackgroundResource(R.drawable.green_shape);
                holder.zf.setTextColor(Color.WHITE);
                holder.price.setTextColor(parent.getContext().getResources().getColor(R.color.green_2ab23c));
            } else {
                holder.zf.setBackgroundResource(R.drawable.orange_shape1);
                holder.zf.setTextColor(Color.WHITE);
                holder.price.setTextColor(parent.getContext().getResources().getColor(R.color.red1));
            }

            holder.clickHomeQutos.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (searchClickListener != null) {
                        searchClickListener.onRegularClick(search.tick, search.name, search.symbols_id, search.type, search.symbol, search.full_symbol, search.price, search.h, search.l, search.o, search.zf, search.yc, search.buy1, search.buy2, search.position, search.service_charge, search.zd);
                    }
                }
            });
        }
        return convertView;
    }


    class ViewHolder {
        @BindView(R.id.name)
        TextView name;
        @BindView(R.id.fullsymbol)
        TextView fullsymbol;
        @BindView(R.id.price)
        TextView price;
        @BindView(R.id.zf)
        Button zf;
        @BindView(R.id.click_home_qutos)
        LinearLayout clickHomeQutos;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
